package com.lc1993929.mr.wc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * Created by LiuChang on 2017/11/3/003.
 */
public class WcJobSubmitter {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //conf.set("mapreduce.framework.name", "local");
        //跨平台提交
        conf.set("mapreduce.app-submission.cross-platform", "true");
        Job job = Job.getInstance(conf);
        //要执行的jar文件
        job.setJarByClass(WcJobSubmitter.class);
        //指定mapper类和reducer类
        job.setMapperClass(WcMapper.class);
        job.setReducerClass(WcReducer.class);
        //指定combiner类处理每个map完成后的数据给reduce
        job.setCombinerClass(WcCombiner.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //指定要解析的文件类型
        job.setInputFormatClass(TextInputFormat.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[1]));
        //指定要输出的文件类型
        job.setOutputFormatClass(TextOutputFormat.class);
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        //设置reduce任务个数
        job.setNumReduceTasks(Integer.parseInt(args[0]));
        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }
}
